package com.tinem.platform.service.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.tinem.platform.module.pojo.vo.error.UserException;
import com.tinem.platform.module.pojo.vo.user.*;
import com.tinem.platform.module.starter.jpa.entity.rbac.RbacUser;
import com.tinem.platform.module.starter.jpa.repository.rbac.RbacUserRepository;
import com.tinem.platform.module.starter.sdk.MessageCode;
import com.tinem.platform.module.starter.web.context.GatewayContext;
import com.tinem.platform.module.commons.util.PasswordUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className MyApi
 * @description 用户维护自身信息
 * @site
 * @company
 * @create 2021-06-10 21:23
 */
@RestController("/my")
@RequestMapping
public class MyApi {

    @Resource
    RbacUserRepository rbacUserRepository;

    @PostMapping("/get.api")
    public RbacUser get(@RequestBody GetReq req) {
        Optional<RbacUser> optional = rbacUserRepository.findById(req.getUuidToId());
        if(optional.isPresent()){
            return optional.get();
        }else{
            return null;
        }
    }
    @PostMapping("/update.api")
    public RbacUser update(@Valid @RequestBody UpdateReq updateReq) {
        RbacUser user = new RbacUser();
        BeanUtil.copyProperties(updateReq,user);
        user.setId(GatewayContext.getUserId());
        return rbacUserRepository.saveAndFlush(user);
    }

    @PostMapping("/updateMobile.api")
    public RbacUser updateMobile(@Valid @RequestBody UpdateMobileReq updateMobileReq, HttpServletRequest request) {
        RbacUser user = rbacUserRepository.findFirstByMobileAndCountryMobileCode(updateMobileReq.getCountryMobileCode(),updateMobileReq.getMobile());
        if(user != null && user.getId().equals(GatewayContext.getUserId())){
            //根据手机号查询到用户不为空，且查询到的用户不是本用户则提示手机号已经绑定其他账号
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_SERVICE_USER_MOBILE);
        }
        user = new RbacUser();
        user.setId(GatewayContext.getUserId());
        user.setMobile(updateMobileReq.getMobile());
        return rbacUserRepository.saveAndFlush(user);
    }

    @PostMapping("/updateEmail.api")
    public RbacUser updateEmail(@Valid @RequestBody UpdateEmailReq updateEmailReq) {
        RbacUser user = rbacUserRepository.findFirstByEmail(updateEmailReq.getEmail());
        if(user != null && user.getId().equals(GatewayContext.getUserId())){
            //根据邮箱查询到用户不为空，且查询到的用户不是本用户则提示邮箱已经绑定其他账号
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_SERVICE_USER_EMAIL);
        }
        user = new RbacUser();
        user.setId(GatewayContext.getUserId());
        user.setEmail(updateEmailReq.getEmail());
        return rbacUserRepository.saveAndFlush(user);
    }

    @Transactional
    @PostMapping("/updatePassword.api")
    public RbacUser updatePassword(@Valid @RequestBody UpdatePasswordReq updatePasswordReq) {
        RbacUser user = rbacUserRepository.getOne(GatewayContext.getUserId());
        if(user.getPassword().equals(PasswordUtil.encode(updatePasswordReq.getPassword()))){
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_SERVICE_USER_PASSWORD);
        }
        user.setPassword(PasswordUtil.encode(updatePasswordReq.getNewPassword()));
        user.setPasswordExpired(updatePasswordReq.getPasswordExpired());
        user.setPasswordErrorTime(new Date(0));
        user.setPasswordErrorNumber(0);
        return rbacUserRepository.saveAndFlush(user);
    }
}
